# from crawler.connection import send_reply
from scrapy.spidermiddlewares.httperror import HttpError
from scrapy.downloadermiddlewares.retry import RetryMiddleware


class HttpErrorCatchMiddleware(RetryMiddleware):

    def process_spider_exception(self, response, exception, spider):
        if isinstance(exception, HttpError):
            task = response.meta['task']
            response_status = response.status
            # 如果多次重试返回状态码为5系错误码，则认定为服务器挂掉了，不在进行重试，否则需要进行状态4重试
            if str(response_status).startswith('5'):
                reply_status = 2
            else:
                reply_status = 4
            error_msg = '多次重试请求失败，状态码为：{}'.format(response.status)
            retries = response.meta.get('retry_times', 0)
            if retries >= self.max_retry_times and "Send Relay Status Is In 2,4,99" not in error_msg:
                pass
                # send_reply(task, reply_status, error_msg)
        return None
